<?php
/*
 * 进云智能生成平台
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');

if(!$_SESSION['wechat_id']){
	exit('参数错误！授权登陆失败！');
}
if($_RQ['code']){
		$wechat=pdo_get('core_account_wechat',array('id'=>$_SESSION['wechat_id'],'uniacid'=>$_SESSION['uniacid']),array('id','appid','appsecret','oldid'));
		$url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$wechat['appid'].'&secret='.$wechat['appsecret'].'&code='.$_RQ['code'].'&grant_type=authorization_code';
		$result=file_get_contents($url);
			if(!$result){
				$result=cfc('http')->ihttp_get($url);
				$result=$result['content'];
			}
			$result=json_decode($result,true);
			if($result['errcode']){
				exit('获取openid失败：'.$result['errmsg']);
			}
			$url='https://api.weixin.qq.com/sns/userinfo?access_token='.$result['access_token'].'&openid='.$result['openid'].'&lang=zh_CN';
			$result=file_get_contents($url);
			if(!$result){
				$result=cfc('http')->ihttp_get($url);
				$result=$result['content'];
			}
			$result=json_decode($result,true);
			if($result['errcode']){
				exit('获取粉丝详情失败：'.$result['errmsg']);
			}
			$fansdata=array(
				'nickname'=>$result['nickname'],
				'avatar'=>$result['headimgurl'],
				'sex'=>$result['sex'],
				'tagid_list'=>$result['tagid_list']?';'.implode(';'.$result['tagid_list']).';':'',
				'pcd'=>$result['country'].$result['province'].$result['city'],
			);
			$_SESSION['openid']=$result['openid'];
			$_SESSION['fans']=array('avatar'=>$result['headimgurl'],'nickname'=>$result['nickname']);
			$fans=pdo_get('core_fans',array('openid'=>$result['openid'],'uniacid'=>$_SESSION['uniacid'],'wxgid'=>$wechat['id']),array('id','mid','nickname','avatar'));
			if($fans){
				if($fans['mid']){
					$member=pdo_get('core_members',array('id'=>$fans['mid'],'uniacid'=>$_SESSION['uniacid']),array('credit1','credit2','nickname','avatar'));
					if(!$member){
						$fansdata['mid']=0;
					}else{
						$_SESSION['uid']=$fans['mid'];
						$_SESSION['member']=$member;
					}
				}
				pdo_update('core_fans',$fansdata,array('openid'=>$result['openid'],'uniacid'=>$_SESSION['uniacid'],'wxgid'=>$wechat['id']));
			}else{
				$fansdata['openid']=$result['openid'];
				$fansdata['uniacid']=$_SESSION['uniacid'];
				$fansdata['unionid']=$result['unionid'];
				$fansdata['wxgid']=$wechat['id'];
				$fansdata['createtime']=TIMESTAMP;
				$fansdata['type']=2;
				if($fansdata['unionid']){
					$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'unionid'=>$fansdata['unionid']),array('id','onemid'));
					$mid=$member['onemid']?:$member['id'];
				}
				if($mid){
					$fansdata['mid']=$mid;
					$_SESSION['uid']=$mid;
					$_SESSION['member']=pdo_get('core_members',array('id'=>$mid,'uniacid'=>$_SESSION['uniacid']),array('credit1','credit2','nickname','avatar'));
				}
				pdo_insert('core_fans',$fansdata);
			}
}
if(!$_SESSION['access_json']){
	header('location:'.SITEROOT.'custom/index.php');
}
if(substr($_SESSION['access_json'],0,4)=='http'){
	header('location:'.$_SESSION['access_json']);
}
header('location:'.SITEROOT.'custom/index.php?'.$_SESSION['access_json']);
exit;